﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Windows.Toolbar.Controls
{
    public class SelectionContainer : UserControl
    {
        #region Events

        /// <summary>
        /// Occurs when the selected item has changed.
        /// </summary>
        public event EventHandler SelectedValueChanged;
        /// <summary>
        /// Raises the <see cref="E:SelectedValueChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="SelectedValueChangedEventArgs"/> instance containing the event data.</param>
        private void OnSelectedValueChanged(EventArgs e)
        {
            EventHandler eventHandler = SelectedValueChanged;

            if (eventHandler != null)
            {
                eventHandler(this, e);
            }
        }

        #endregion

        #region Properties

        public static readonly DependencyProperty SelectedValueProperty =
            DependencyProperty.Register("SelectedValue", typeof(object), typeof(SelectionContainer), new PropertyMetadata(null, new PropertyChangedCallback(OnSelectedValueChanged)));
        public object SelectedValue
        {
            get { return (object)GetValue(SelectedValueProperty); }
            set { SetValue(SelectedValueProperty, value); }
        }

        private static void OnSelectedValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var owner = o as SelectionContainer;
            if (owner != null)
            {
                owner.OnSelectedValueChanged();
            }
        }

        protected virtual void OnSelectedValueChanged()
        {
            OnSelectedValueChanged(EventArgs.Empty);
        }

        #endregion
    }
}
